//
//  PointerDemo.c
//  CProject
//
//  Created by XMYY-19 on 2018/8/29.
//  Copyright © 2018年 黄小刚. All rights reserved.
//

#include "PointerDemo.h"
#include <malloc/malloc.h>
#include <stdlib.h>

void pointDemo1(void) {
    
    int * p;
    int i = 5;
    
    p = &i;
    
    printf("*p is %d\n",*p);
    
}


void swap(int *p1, int * p2){
    
    int *temp ;
    temp = p1;
    p1 = p2;
    p2 = temp;
}

void pointDemo2(void) {
    
    int a = 1;
    int b = 3;
    
    swap(&a, &b);
    
    printf("a = %d, b = %d\n",a,b);
    
}


// -------- 返回多个值 ------

void mySwap(int a, int b, int * p, int * q) {
    *p = b;
    *q = a;
}

void pointDemo3(void) {
    
    
    int a = 3;
    int b = 5;
    
    int s;
    int t;
    
    mySwap(a,b,&s,&t);
    
    printf("s = %d, t = %d\n",s,t);
}


// -------- 一维数组 ------

// 遍历数组
void getArr (int *arr, int len) {
    
    for (int i = 0; i < len; i ++) {
     
        /*
          printf("arr[%d] = %d\n",i,*(arr + i));
    等价于:
         printf("arr[%d] = %d\n",i,arr[i]));
         */
        printf("arr[%d] = %d\n",i,arr[i]);
        
    }
}

void pointDemo5(void) {
    
    int a[5] = {1,2,3,4,5};

    int b[6] = {11,21,33,4,50,16};

    getArr(a, 5);
    getArr(b, 6);
}

void pointDemo6(void) {
    
    char ch = 'A';
    int i = 99;
    double x = 66.6;
    char * p = &ch;
    int * q = &i;
    double * r = &x;
    
    printf("%lu %lu %lu\n",sizeof(p),sizeof(q),sizeof(r));
}


// ------- 动态内存分配 ------

//
//void mallocTest(void) {
//
//    int * p = (int *)malloc(4);
//    * p = 10;
//    free(p);
//}

void fMalloc(int * a) {

    * a = 30;
}

void mallocFunc (void) {

    int * p = (int *)malloc(sizeof(int));
    *p = 20;
    printf("*p = %d\n",*p);
    printf("%#X\n",p);
    fMalloc(p);
    printf("after *p = %d\n",*p);
    free(p);
    
}

void pointDemo7(void) {

    mallocFunc();
}

// 动态构造一维数组

void myGetArr(void) {
    
    // 创建动态内存分配空间
    int *arrP = (int *)malloc(5 * sizeof(int));
    
    for (int i = 0; i < 5; i ++) {
    
        *(arrP + i) = i + 2;
    }
    
    for (int i = 0; i < 5; i ++) {
        printf("arr val Info :%d\n", * (arrP + i));
    }
    
}

void pointDemo8(void) {
    
    myGetArr();
}

// 多级指针
void pointDemo9_0(void) {
    
    int i = 20;
    int *p;
    p = &i;
    int ** q = &p;
    int *** r = &q;
    
    //
    printf("&i = %#X\n",&i);
    printf("*p = %#X\n",p);
    printf("&p = %#X\n",&p);
    printf("**q = %#X\n",q);
    
}


void pointDemo9_1_0 (int ** a) {
    
    ** a = 20;
}

void pointDemo9_1 (void) {
    
    int i = 30;
    int *p = &i;
//    int **q = &p;
    
    pointDemo9_1_0(&p);
    
    printf("after i = %d",i);
    
}

void pointDemo9_2_0 (int ** p) {
    
    ** p = 30;
    
}


void pointDemo9_2 (void) {
    
    int * i = (int *)malloc(sizeof(int));// 等价于 获取动态分配变量后的变量的内存地址,可以理解为新建一个指针变量指向 新开辟的内存空间
    *i = 20; // 获取对应内存地址的值
    
    printf("*i = %d\n",*i);
    int ** p = &i;
    
    pointDemo9_2_0(p);
    printf("after *i = %d\n",*i);
    
}


void pointDemo9(void) {
    pointDemo9_2();
}

// -------- 跨函数使用内存 ------

void pointDemo10_0 (int *** p) {
    int i = 13;
    **p = &i;
//    *p = &i;
}

void pointDemo10_1 (void) {
    
    int * i = (int *)malloc(sizeof(int));
    *i = 30;
    
    int *p = i;
    
    int **q = &p;
    
    int ***x = &q;
    
    pointDemo10_0(x);
    
    printf("**p = %d\n",*p);
}

void pointDemo10_2_0(int ** q) {
    int i = 23;
    
    *q = &i;
}

void pointDemo10_2 (void) {
    
    int *p;
    pointDemo10_2_0(&p);
    
    printf("*p = %d\n", *p);// 语法没有问题，但是逻辑有问题，存在指针越界的情况,因为执行完pointDemo10_2_0函数，i就释放掉了，p可以保存i的地址，但是无权访问i的内容。一旦访问就存在访问一个它无权访问的内容。读出来的值，其实是个旧的垃圾值。即静态变量不能跨函数使用。
    
    
}

void pointDemo10(void) {
    
    pointDemo10_2();
}

// ------- 冒泡排序 --------




void sort(int * a, int lenght) {
    
    // 外循环决定确定每个具体输出值要执行多少次（比如我有6个值，我要保证每一轮要确定一个最大值）
    for (int i = 0; i < lenght - 1; i++) {
        
        // 内循环决定确定每个最大值需要执行的次数（这个要保证每确定一个最大值要交换多少次）
        for (int j = 0; j <lenght - 1 - i; j ++) {
            
            if (a[j] > a[j + 1]) {
                
                int temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
}

void pointDemo11(void) {
    
    
    // 定义一个数组
    int  numArr[6] = {10,101,23,56,32,89};
    
    sort(numArr,6);
    
    for (int i = 0; i < 6; i ++) {
        
        printf("numArr => %d\n",numArr[i]);
        
    }
    
    
}




























